package com.sunny.ynhz.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.sunny.ynhz.R;

import butterknife.ButterKnife;

import rx.Subscription;

/**
 * Created by _huhuan
 * Date:2016/3/30
 * Time:11:30
 */
public abstract class BaseFragment extends Fragment {
    public View mContentView;
    public View load_view;
    public Subscription subscription;
    public LayoutInflater inflater;
    public FragmentActivity activity;
    private TextView tv_load;
    private View view;
    private View progress_view;
    public Bundle savedInstanceState;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflater = LayoutInflater.from(getActivity());

        mContentView = inflater.inflate(setLayoutResourceID(), null);//setContentView(inflater, container);
        load_view = mContentView.findViewById(R.id.root_load_view);
        this.inflater = inflater;
        view = mContentView.findViewById(R.id.root_load_view);
        if (load_view != null) {
            tv_load = (TextView) view.findViewById(R.id.tv_load);
            progress_view = view.findViewById(R.id.pro_load);
            if (tv_load != null) {
                tv_load.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        resLoad();
                    }
                });
            }
        }
        this.savedInstanceState = savedInstanceState;
        ButterKnife.bind(this, mContentView);


        init();
        setUpView();
        return mContentView;
    }

    protected abstract int setLayoutResourceID();


    protected void setUpData() {


    }

    public void doActivity(Class<?> c) {
        Intent intent = new Intent(getActivity(), c);
        startActivity(intent);

    }

    public void doActivity(Class<?> c, String tag, String content) {
        Intent intent = new Intent(getActivity(), c);
        intent.putExtra(tag, content);
        startActivity(intent);

    }

    protected void init() {

    }


    protected void setUpView() {
    }

    public void setRetLoad() {

    }

    public void canceNetLoad() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }

    @Override
    public void onDestroyView() {
        canceNetLoad();
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected <T extends View> T $(int id) {
        return (T) mContentView.findViewById(id);
    }

    protected View getContentView() {
        return mContentView;
    }

    //内部加载
    public void loadFail() {
        tv_load.setClickable(true);
        tv_load.setText("点击重新加载");
        progress_view.setVisibility(View.GONE);
    }

    public void loadSuccess() {
        view.setVisibility(View.GONE);
    }

    public void startLoad() {
        tv_load.setClickable(false);
        view.setVisibility(View.VISIBLE);
        tv_load.setText("正在加载");
        progress_view.setVisibility(View.VISIBLE);

    }

    public void loadEmply() {
        tv_load.setClickable(true);
        tv_load.setText("暂无数据");
        view.setVisibility(View.VISIBLE);
        progress_view.setVisibility(View.GONE);
    }

    //重新加载
    public void resLoad() {
        startLoad();

    }

    public void doAcitivity(Intent intent) {
        startActivity(intent);
    }

}
